function [irf,gradIrf] = genIRF(Q,jshock,vma,N,neg)
% Generate IRF of the ith variable to the jth structural shock at the hth
% horizon. Also generates analytical gradient of the IRF.
% Inputs:
% Q: vectorised rotation matrix
% jshock: index of structural shock of interest
% vma: row vector of coefficients in VMA representation (up to rotation)
% for ith variable at hth horizon
% N: number of variables
% neg: if neg = -1, return negative of IRF; if neg = 1, return positive

irf = neg*vma*Q((jshock-1)*N+1:jshock*N); % IRF of ith variable to jth shock
gradIrf = zeros(N^2,1);
gradIrf((jshock-1)*N+1:jshock*N) = neg*vma'; % Gradient 

end